<?php
class CacheProfiler
{
       private static $_key_misses = array();
       private static $_key_profiler_hits = array();
       private static $_key_profiler_misses = array();
       public static $_total_misses = 0;
       public static $_total_hits = 0;
       public static $_total_time = 0;

       public static function addKeyMisses($key)
       {
	       if(!self::checkKeyMisses($key))
		       self::$_key_misses[$key] = "1";
       }

       public static function removeKeyMisses($key)
       {
           
	       if(self::checkKeyMisses($key))
	       {
		       unset(self::$_key_misses[$key]);
	       }
       }

       public static function getCacheProfiles()
       {
	       return array_merge(self::$_key_profiler_hits,self::$_key_profiler_misses);
       }

       public static function getTotalMissesCache()
       {
	       return self::$_total_misses;
       }

       public static function getTotalHitsCache()
       {
	       return self::$_total_hits;
       }

       public static function getTotalEllapsedTime()
       {
	       return self::$_total_time;
       }

       public static function getPercentMissesCache()
       {
           $total = '';
	       if($total == 0) return 0;
	       $total = self::$_total_hits + self::$_total_misses;
	       $percent = self::$_total_misses / $total;
	       return $percent * 100;
       }

       public static function getPercentHitsCache()
       {
           $total = '';
	       if($total == 0) return 0;
	       $total = self::$_total_hits + self::$_total_misses;
	       $percent = self::$_total_hits / $total;
	       return $percent * 100;
       }

       public static function checkKeyMisses($key)
       {
	       if(isset(self::$_key_misses[$key])) return true;
	       else return false;
       }

       public static function pushToCacheProfiler($key, $result, $starttime, $endtime)
       {
       	   $debug = Globals::isDebug();
       	   if($debug == true)
       	   {
       	   		$_cache_result = new ResultCache();
		       $_cache_result->key = $key;
		       $diff = ($endtime - $starttime);
		       self::$_total_time += $diff;
		       $_cache_result->ellapsedtime = $diff;
		       if(is_null($result))
		       {
			       self::$_total_misses++;
			       $_cache_result->result = 0;
			       self::$_key_profiler_misses[] = $_cache_result;
		       }
		       else
		       {
			       self::$_total_hits++;
			       $_cache_result->result = 1;
			       self::$_key_profiler_hits[] = $_cache_result;
		       }
       	   }
       }
}
?>
